﻿#region using
using Microsoft.Xna.Framework;
using AwesomeTanks.Sources.GameElements.TankElements;
using AwesomeTanks.Sources.Media;
using AwesomeTanks.Sources.GameElements.MapElements;
using AwesomeTanks.Sources.Shape;

#endregion

namespace AwesomeTanks.Sources.Collision
{
    /// <summary>
    /// 
    /// </summary>
    public class TankCollisions
    {
        #region Methods
        /// <summary>
        /// Check the collision pixel by pixel between two tanks.
        /// </summary>
        /// <param name="tank1">The tank1.</param>
        /// <param name="tank2">The tank2.</param>
        /// <param name="gameTime">The game time.</param>
        public static void TankCollision(Tank tank1, Tank tank2, GameTime gameTime)
        {
            Matrix tank1Matrix = Matrix.CreateTranslation(-tank1.Origin.X, -tank1.Origin.Y, 0)
                * Matrix.CreateRotationZ(tank1.Rotation)
                * Matrix.CreateTranslation(tank1.Position.X, tank1.Position.Y, 0);

            Matrix tank2Matrix = Matrix.CreateTranslation(-tank2.Origin.X, -tank2.Origin.Y, 0)
                * Matrix.CreateRotationZ(tank2.Rotation)
                * Matrix.CreateTranslation(tank2.Position.X, tank2.Position.Y, 0);

            Vector2 collisionPoint = CollisionManager.TexturesCollide(Art.BaseTankColorArray, tank1Matrix, Art.BaseTankColorArray, tank2Matrix);

            if (collisionPoint.X > -1)
            {
                tank1.Position = tank1.OldPosition;
                tank1.Speed = -tank1.OldSpeed;
                tank1.Rotation = tank1.OldRotation;
                tank1.ExhaustFire = false;
                tank2.Position = tank2.OldPosition;
                tank2.Speed = -tank2.OldSpeed;
                tank2.Rotation = tank2.OldRotation;
                tank2.ExhaustFire = false;
                tank1.Update(gameTime);
                tank2.Update(gameTime);
            }
        }

        /// <summary>
        /// Check the collisio between a tank and a wall.
        /// </summary>
        /// <param name="tank">The tank.</param>
        /// <param name="wall">The wall.</param>
        /// <param name="gameTime">The game time.</param>
        public static void WallCollision(Tank tank, Wall wall, GameTime gameTime)
        {
            Rectangle r = new Rectangle((int)wall.Position.X, (int)wall.Position.Y, (int)wall.Scale.X, (int)wall.Scale.Y);
            foreach (Circle circle in tank.HitboxList)
            {
                if (circle.Intersectsrectangle(r))
                {
                    tank.ExhaustFire = false;

                    if (tank.Position.X > wall.Position.X && tank.Position.X < wall.Position.X + wall.Scale.X)
                    {
                        tank.Speed = new Vector2(tank.Speed.X, 0);
                    }
                    else if (tank.Position.Y > wall.Position.Y && tank.Position.Y < wall.Position.Y + wall.Scale.Y)
                    {
                        tank.Speed = new Vector2(0, tank.Speed.Y);
                    }
                    else
                    {
                        if (tank.Speed.Y == 0 || tank.Speed.X == 0)
                        {
                            tank.Speed = Vector2.Zero;
                        }
                    }
                    tank.Position = tank.OldPosition;
                    tank.Update(gameTime);
                }
            }
        }


        /// <summary>
        /// Tanks collision.
        /// </summary>
        /// <param name="tank1">The tank1.</param>
        /// <param name="tank2">The tank2.</param>
        /// <returns></returns>
        public static bool TankCollision(Tank tank1, Tank tank2) {

            foreach (Circle circle1 in tank1.HitboxList)
            {
                foreach (Circle circle2 in tank2.HitboxList)
                {
                    if(circle1.IntersectsCircle(circle2))
                        return true;
                }
            }
            return false;
        }
        #endregion
    }
}
